import pandas as pd
from sqlalchemy import create_engine
from urllib.parse import quote_plus 
import talib

# 配置 MySQL 连接信息
password = quote_plus('NewPass123!@#')
engine = create_engine(f'mysql+pymysql://root:{password}@10.54.1.6:13306/zjjk')

# 从 tushare_stock_basic 表中读取数据
table_name = 'tushare_300476'
query = f"SELECT * FROM {table_name} order by trade_date"
data = pd.read_sql(query, engine)

# 假设表中有 close 列，用于计算 MACD
if 'close' in data.columns:
    macd, signal, hist = talib.MACD(data['close'])
    data['macd'] = macd
    data['signal'] = signal
    data['hist'] = hist
    print("MACD 计算完成，结果已添加到数据中")
else:
    print("数据中不存在 'close' 列，无法计算 MACD")

# 遍历所有的股票编码
for index, row in data.iterrows():
    stock_code = row['ts_code']  # 假设股票编码列名为 ts_code
    print(stock_code,row['macd'],row['signal'],row['hist'])

print(data['trade_date'][0])